<template>
	<view class="wrap">
		<view class="key-input">
			<view class="title">输入验证码</view>
			<view class="tips">验证码已发送至 +{{tel}}</view>
			<u-message-input :focus="true" :value="code" @change="change" @finish="finish" mode="bottomLine" :maxlength="maxlength"></u-message-input>
			<text :class="{ error: error }">验证码错误，请重新输入</text>
			<!-- <view class="captcha">
				<text :class="{ noCaptcha: show }" @tap="noCaptcha">收不到验证码点这里</text>
				<text :class="{ regain: !show }">{{ second }}秒后重新获取验证码</text>
			</view> -->
		</view>
		<u-toast :type="type" ref="uToast"></u-toast>
		<!-- <u-alert-tips @close="close" :closeAble="closeAble" :show="alertShow"
		:type="type" :title="title" :description="description" :show-icon="showIcon"></u-alert-tips> -->
	</view>
</template>

<script>
	import utils from "../../utils/utils.js"
	import store from '@/store/index'
export default {
	data() {
		return {
			maxlength: 6,
			code: '',
			second: 60,
			show: false,
			error: false,
			alertShow:false,
			title: '短信模拟通知',
			description: "【智慧消防】用户，您好，您此次登录验证码为",
			show: true,
			closeAble: true,
			showIcon: false,
			type: 'warning',
		};
	},
	computed: {
		tel(){
			return this.$store.getters.tel
		}
	},
	onLoad() {
		// this.getCaptcha()
		let interval = setInterval(() => {
			this.second--;
			if (this.second <= 0) {
				//this.show = true;
				clearInterval(interval);
			}
		}, 1000);
		this.init()
	},
	methods: {
		init(){
			this.description = this.description + utils.getStorageSync("code")
			this.alertShow = true
		},
		// 收不到验证码选择时的选择
		noCaptcha() {
			uni.showActionSheet({
				itemList: ['重新获取验证码', '接听语音验证码'],
				success: function(res) {
					
				},
				fail: function(res) {
					
				}
			});
		},
		// change事件侦听
		change(value) {
			// console.log('change', value);
		},
		// 输入完验证码最后一位执行
		finish(value) {
			// userLoginByCode
			utils.request("/api/admin/auth/userLoginByCode",{mobile:this.tel,code:value},"post","app").then(res=>{
				store.dispatch("user/setTokenInfo").then(res=>{
					this.utils.request("/api/app/user/getUserInfo", {}, "get", "app", true).then(res => {
						if(res.data.roleCode == "XZ-ADMIN" || res.data.roleCode == "XZ-YH"){
							//消灾企业用户
							uni.redirectTo({
								url:"/pages/index/index_xz"
							})
						} else if(res.data.roleCode == "WB-ADMIN" || res.data.roleCode == "WB-YH"){
							//维保企业用户
							uni.redirectTo({
								url:"/pages/index/index_wb"
							})
						}else if(res.data.roleCode == "JC-ADMIN" || res.data.roleCode == "JC-YH"){
							//维保企业用户
							uni.redirectTo({
								url:"/pages/index/index_jc"
							})
						}else{
							//其他企业用户
							uni.redirectTo({
								url:"/pages/index/index_qt"
							})
						}
					}).catch(error => {
						console.log(error)
					})
				})
			}).catch(error=>{
				console.log(error)
				this.$refs.uToast.show({
					title: error.data || error.msg,
					position: "center",
					type: "error",
					icon: false,
					url: "",
				});
			})
		},
		close() {
			this.alertShow = false;
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	padding: 80rpx;
}

.box {
	margin: 30rpx 0;
	font-size: 30rpx;
	color: 555;
}

.key-input {
	padding: 30rpx 0;
	text {
		display: none;
	}
	.error {
		display: block;
		color: red;
		font-size: 30rpx;
		margin: 20rpx 0;
	}
}

.title {
	font-size: 50rpx;
	color: #333;
}

.key-input .tips {
	font-size: 30rpx;
	color: #333;
	margin-top: 20rpx;
	margin-bottom: 60rpx;
}
.captcha {
	color: $u-type-warning;
	font-size: 30rpx;
	margin-top: 40rpx;
	.noCaptcha {
		display: block;
	}
	.regain {
		display: block;
	}
}
</style>
